package com.dianping.cat.home.service.client.transform;

import com.dianping.cat.home.service.client.IVisitor;
import com.dianping.cat.home.service.client.entity.ClientReport;
import com.dianping.cat.home.service.client.entity.Domain;
import com.dianping.cat.home.service.client.entity.Method;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;


public class DefaultNativeBuilder
  implements IVisitor
{
  private IVisitor m_visitor;
  private DataOutputStream m_out;

  public DefaultNativeBuilder(OutputStream out) { this(out, null); }


  public DefaultNativeBuilder(OutputStream out, IVisitor visitor) {
    this.m_out = new DataOutputStream(out);
    this.m_visitor = (visitor == null) ? this : visitor;
  }

  public static byte[] build(ClientReport clientReport) {
    ByteArrayOutputStream out = new ByteArrayOutputStream(8192);

    build(clientReport, out);
    return out.toByteArray();
  }


  public static void build(ClientReport clientReport, OutputStream out) { clientReport.accept(new DefaultNativeBuilder(out)); }



  public void visitClientReport(ClientReport clientReport) {
/*  41 */     writeTag(63, 0);

/*  43 */     if (clientReport.getDomain() != null) {
/*  44 */       writeTag(1, 1);
/*  45 */       writeString(clientReport.getDomain());
    }

/*  48 */     if (clientReport.getStartTime() != null) {
/*  49 */       writeTag(2, 1);
/*  50 */       writeDate(clientReport.getStartTime());
    }

/*  53 */     if (clientReport.getEndTime() != null) {
/*  54 */       writeTag(3, 1);
/*  55 */       writeDate(clientReport.getEndTime());
    }

/*  58 */     if (!clientReport.getDomains().isEmpty()) {
/*  59 */       writeTag(33, 2);
/*  60 */       writeInt(clientReport.getDomains().size());

/*  62 */       for (Domain domain : clientReport.getDomains().values()) {
/*  63 */         domain.accept(this.m_visitor);
      }
    }

/*  67 */     writeTag(63, 3);
  }


  public void visitDomain(Domain domain) {
/*  72 */     if (domain.getId() != null) {
/*  73 */       writeTag(1, 1);
/*  74 */       writeString(domain.getId());
    }

/*  77 */     writeTag(2, 0);
    writeLong(domain.getTotalCount());

/*  80 */     writeTag(3, 0);
/*  81 */     writeLong(domain.getFailureCount());

/*  83 */     writeTag(4, 0);
/*  84 */     writeDouble(domain.getFailurePercent());

/*  86 */     writeTag(5, 0);
/*  87 */     writeDouble(domain.getSum());

/*  89 */     writeTag(6, 0);
/*  90 */     writeDouble(domain.getAvg());

/*  92 */     if (!domain.getMethods().isEmpty()) {
/*  93 */       writeTag(33, 2);
/*  94 */       writeInt(domain.getMethods().size());

/*  96 */       for (Method method : domain.getMethods().values()) {
/*  97 */         method.accept(this.m_visitor);
      }
    }

/* 101 */     writeTag(63, 3);
  }


  public void visitMethod(Method method) {
/* 106 */     if (method.getId() != null) {
/* 107 */       writeTag(1, 1);
/* 108 */       writeString(method.getId());
    }

/* 111 */     if (method.getService() != null) {
/* 112 */       writeTag(2, 1);
/* 113 */       writeString(method.getService());
    }

/* 116 */     writeTag(3, 0);
/* 117 */     writeLong(method.getTotalCount());

/* 119 */     writeTag(4, 0);
/* 120 */     writeLong(method.getFailureCount());

/* 122 */     writeTag(5, 0);
/* 123 */     writeDouble(method.getFailurePercent());

/* 125 */     writeTag(6, 0);
/* 126 */     writeDouble(method.getSum());

/* 128 */     writeTag(7, 0);
/* 129 */     writeDouble(method.getAvg());

/* 131 */     writeTag(8, 0);
/* 132 */     writeDouble(method.getQps());

/* 134 */     writeTag(9, 0);
/* 135 */     writeDouble(method.getTimeout());

/* 137 */     writeTag(63, 3);
  }

  private void writeDate(Date value) {
    try {
/* 142 */       writeVarint(value.getTime());
/* 143 */     } catch (Exception e) {
/* 144 */       throw new RuntimeException(e);
    }
  }

  private void writeDouble(double value) {
    try {
/* 150 */       writeVarint(Double.doubleToLongBits(value));
/* 151 */     } catch (Exception e) {
/* 152 */       throw new RuntimeException(e);
    }
  }

  private void writeInt(int value) {
    try {
/* 158 */       writeVarint(value & 0xFFFFFFFFL);
/* 159 */     } catch (Exception e) {
/* 160 */       throw new RuntimeException(e);
    }
  }

  private void writeLong(long value) {
    try {
/* 166 */       writeVarint(value);
/* 167 */     } catch (Exception e) {
/* 168 */       throw new RuntimeException(e);
    }
  }

  private void writeString(String value) {
    try {
/* 174 */       this.m_out.writeUTF(value);
/* 175 */     } catch (IOException e) {
/* 176 */       throw new RuntimeException(e);
    }
  }

  private void writeTag(int field, int type) {
    try {
/* 182 */       this.m_out.writeByte((field << 2) + type);
/* 183 */     } catch (IOException e) {
/* 184 */       throw new RuntimeException(e);
    }
  }

  protected void writeVarint(long value) throws IOException {
    while (true) {
/* 190 */       if ((value & 0xFFFFFFFFFFFFFF80L) == 0L) {
/* 191 */         this.m_out.writeByte((byte)(int)value);
        return;
      }
/* 194 */       this.m_out.writeByte((byte)(int)value & 0x7F | 0x80);
/* 195 */       value >>>= 7;
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/classes/!/com/dianping/cat/home/service/client/transform/DefaultNativeBuilder.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */